<template>
	<view>
		<!-- 主体 -->
		<view class="page page-bottom-bar book-group order-detail">
			<!-- 状态信息 -->
			<view class="book-status flex-row-center">
				<image class="icon" src="https://static.newbeaconhotels.com/app/hotel/group/icon-wait4.png"></image>
				<view>{{dataobj.name_is?dataobj.name_is:''}}</view>
			</view>
			<!-- 物流信息 -->
			<!-- 			<view class="book-info flex-row-between box-shadow" style="margin-top: 0;">
				<view class="flex-1">
					<view class="p1">
						<text class="label">物流公司：</text>
						<text>百世快递</text>
					</view>
					<view class="p2">
						<text class="label">物流单号：</text>
						<text>srdrt574245548</text>
					</view>
				</view>
				<uni-icons class="fr" type="arrowright" size="12" color="#9a9a9a"></uni-icons>
			</view> -->
			<!-- 收货人信息 -->
			<view class="book-info box-shadow" v-if="dataobj.address_id">
				<view class="p1 clearfix">
					<text class="label">收货人：</text>
					<text>{{dataobj.address_user_name}}</text>
					<text class="fr">{{dataobj.address_user_phone}}</text>
				</view>
				<view class="p2">
					<text class="label">收货地址：</text>
					<text>{{dataobj.province}}{{dataobj.city}}{{dataobj.region}}{{dataobj.address_user_addr}}</text>
				</view>
			</view>
			<!-- 金额信息 -->
			<view class="book-info box-shadow">
				<view class="p1 clearfix">
					<text class="label">订单编号：</text>
					<text>{{dataobj.order_no?dataobj.order_no:''}}</text>
					<text class="fr font-link" @click="onCopy(dataobj.order_no)">复制</text>
				</view>
				<view class="p2">
					<text class="label">下单时间：</text>
					<text>{{dataobj.createtime_text?dataobj.createtime_text:''}}</text>
				</view>
			</view>
			<!-- 订单信息 -->
			<view class="book-detail font14 box-shadow">
				<view class="goods flex-row-start">
					<view class="left bg-cover" :style="{backgroundImage: 'url(' + goodsobj.logo + ')'}"></view>
					<view class="right flex-1 flex-column-between">
						<view class="font14">{{goodsobj.goods_name?goodsobj.goods_name:''}}</view>
						<view class="font12 font-969696">规格：{{goodsobj.spec_type==10?'默认':goodsobj.goods_attr}}</view>
						<view class="font12 flex-row-between">
							<view class="font-969696">x{{goodsobj.total_num?goodsobj.total_num:''}}</view>
							<view>￥{{goodsobj.goods_price?goodsobj.goods_price:''}}</view>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="font-999999">商品总额:</view>
					<view>￥{{dataobj.total_price?dataobj.total_price:0}}</view>
				</view>
				<view class="item">
					<view class="font-999999">商品数量:</view>
					<view>{{goodsobj.total_num?goodsobj.total_num:0}}</view>
				</view>
				<view class="item">
					<view class="font-999999">运费:</view>
					<view>￥{{dataobj.express_price?dataobj.express_price:0}}</view>
				</view>
				<view class="item" v-if="dataobj.is_chief==1">
					<view class="font-999999">团长优惠:</view>
					<view>-￥ {{dataobj.chief_reduce?dataobj.chief_reduce:0}}</view>
				</view>
				<view class="item">
					<view class="font-999999">优惠券:</view>
					<view>-￥ {{dataobj.member_coupon_price?dataobj.member_coupon_price:0}}</view>
				</view>
				<view class="item nob font15">
					<view>合计:</view>
					<view class="font-assist">￥{{dataobj.pay_status==2?dataobj.total_price:dataobj.pay_price}}</view>
				</view>
			</view>
		</view>
		<!-- 支付倒计时 -->
		<view class="time_end timeE_flex flex-row-start font-ff3c37 font11" v-if="timeobj.show">
			<text>温馨提示:订单将于</text>
			<text>{{timeobj.day>0?timeobj.day+'天':''}}</text>
			<text>{{timeobj.hour>0?timeobj.hour+'小时':''}}</text>
			<text>{{timeobj.minute}}分</text>
			<text>{{timeobj.second}}秒</text>
			<text>之后自动取消</text>
		</view>
		<!-- 合计信息 -->
		<view class="book-bar">
			<view class="book-bar-inner flex-row-end">
				<view class="btn" v-if="dataobj.pay_status == 2 && dataobj.order_status==10"
					@click="goNext('/pages/group/book?code='+dataobj.order_no)">去支付</view>
				<view class="btn" v-if="dataobj.pay_status == 2 && dataobj.order_status==10" @click="Ontrue('qx')">取消订单
				</view>
				<view class="btn" v-if="dataobj.pay_status == 1 && dataobj.order_status==10" @click="Ontrue('tk')">申请退款
				</view>
				<!--  -->
				<view class="btn" v-if="dataobj.after_is"
					@click="goNext(dataobj.refund_status==10?'/pages/group/afterSale?code='+code:'/pages/group/after?code='+code)">
					{{dataobj.refund_name}}
				</view>
				<view class="btn"
					v-if="dataobj.pay_status == 1 && (dataobj.order_status==20||dataobj.order_status==30||dataobj.order_status==40||dataobj.order_status==50)"
					@click="goNext('/pages/group/driver?id='+dataobj.id)">查看物流</view>
				<view class="btn" v-if="dataobj.pay_status == 1 && dataobj.order_status==20" @click="Ontrue('sh')">确定收货
				</view>
				<view class="btn" v-if="dataobj.pay_status == 1 && dataobj.order_status==30"
					@click="goNext('/pages/group/comment?code='+code)">去评论
				</view>
				<!-- <view class="btn">申请售后</view>
				<view class="btn">确认收货</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '', //订单code
				dataobj: {}, //总数据
				goodsobj: {}, //商品数据
				imgFullUrl: this.$Config.http_static, //图片地址
				spell_pay_time: 0, //共配时间
				timer: null, // 定时器
				nowTime: '', //当前时间
				timeobj:{},//倒计时时间
			}
		},
		onLoad: function(options) {
			let query = {}
			if (this.$Route.query.scene) {
				query = this.$base.OnWxoptions(this.$Route.query.scene)
			} else {
				query = options
			}
			this.code = query.code
			//公共配置
			this.OngroupBuy()

		},
		onHide: function() {
			// 清空定时器
			// clearInterval(this.timer)
			// this.timer = null
		},
		onUnload: function() {
			// 清空定时器
			// clearInterval(this.timer)
			// this.timer = null
		},
		methods: {
			//公共配置
			OngroupBuy() {
				this.$Http({
					url: 'groupBuy',
					token: true,
					data: {},
				}).then(res => {
					if (res.code == 1) {
						this.spell_pay_time = res.data.spell_pay_time
						// 商品详情
						this.OnDetails()
					}
				});
			},
			// 商品详情
			OnDetails() {
				this.$Http({
					url: 'detailsOrder',
					token: true,
					type: 'POST',
					data: {
						order_code: this.code
					},
				}).then(res => {
					if (res.code == 1) {
						// pay_status 1=>支付 2=>未支付
						if (res.data.pay_status == 2) {
							if (res.data.order_status == 10) {
								if(res.data.is_spell == 0){
									this.getMessage()
									clearInterval(this.timer)
									this.timer = setInterval(this.getMessage, 1000)
								}else{
									this.timeobj = {}
								}
								res.data.name_is = '订单待支付'
							}
							if (res.data.order_status == 60) {
								res.data.name_is = '订单已取消'
							}
							res.data.after_is = false
						} else {
							//10进行中
							if (res.data.order_status == 10) {
								// is_spell是否拼团购买 1=>是 2=>不是
								if (res.data.is_spell == 1) {
									if (res.data.spell_info.status == 'normal') {
										res.data.name_is = '拼团中，还差人' +
											this.$base.numSub(res.data.spell_info.spell_number, res.data.spell_info
												.count_user) +
											'成团'
										res.data.down_is = true
									}
									if (res.data.spell_info.status == 'hidden') {
										res.data.name_is = '拼团异常'
									}
									if (res.data.spell_info.status == 'end') {
										res.data.name_is = '拼团成功,待发货'
									}
									if (res.data.spell_info.status == 'deleted') {
										res.data.name_is = '很遗憾,拼团失败!'
									}
								} else {
									res.data.name_is = '待发货'
								}
								res.data.after_is = false
							}
							if (res.data.order_status == 20) {
								res.data.name_is = '订单已发货'
								res.data.after_is = true
							}
							if (res.data.order_status == 30) {
								res.data.name_is = '订单待评价'
								res.data.after_is = true
							}
							if (res.data.order_status == 40) {
								res.data.name_is = '订单已评价'
								res.data.after_is = true
							}
							if (res.data.order_status == 50) {
								res.data.name_is = '订单已完成'
								res.data.after_is = true
							}
							if (res.data.order_status == 60) {
								res.data.name_is = '订单已取消'
								res.data.after_is = false
							}
							if (res.data.order_status == 70) {
								res.data.name_is = '订单已退款'
								res.data.after_is = false
							}
							//售后
							if (res.data.refund_status == 10) {
								res.data.refund_name = '申请售后'
							} else {
								res.data.refund_name = '售后详情'
							}
						}
						res.data.order_goods.logo = this.imgFullUrl + res.data.order_goods.logo
						this.dataobj = res.data
						this.goodsobj = res.data.order_goods
						this.nowTime = Math.round(new Date().getTime());
						
					}
				});
			},
			//计时器
			getMessage() {
				var end = this.spell_pay_time / 2 * 1000 + (this.dataobj.createtime*1000)
				var ms = end - this.nowTime;
				var timeobj = {}
				if (ms > 0) {
					this.nowTime = this.nowTime + 1000
					var day = Math.floor(ms / (1000 * 60 * 60 * 24));
					var hour = Math.floor(ms / (1000 * 60 * 60)) % 24;
					if (day > 0) {
						hour = (day * 24) + hour
					}
					var minute = Math.floor(ms / (1000 * 60)) % 60;
					var second = Math.floor(ms / 1000) % 60;
					minute=minute>9?minute:'0'+minute.toString()
					second=second>9?second:'0'+second.toString()
					timeobj = {
						'day': day,
						'hour': hour,
						'minute': minute,
						'second': second,
						'show': true
					}
				} else {
					timeobj = {}
					clearInterval(this.timer)
					this.dataobj.order_status = 60
					this.dataobj.name_is = '订单已自动取消'
				}
				this.$forceUpdate()
				this.timeobj = timeobj
			},
			// 复制到粘贴板
			onCopy(content) {
				// #ifdef H5
				let result = this.$base.h5Copy(content)
				if (result == false) {
					uni.showToast({
						title: '不支持',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: '复制成功'
					})
				}
				// #endif

				// #ifndef H5
				uni.setClipboardData({
					data: content,
					success: () => {
						uni.showToast({
							title: '复制成功'
						})
					}
				})
				// #endif
			},
			// 确认收货 退款
			Ontrue(name) {
				let content = ''
				let url = ''
				let that = this
				//确认收货
				if (name == 'sh') {
					content = '您确定要收货吗'
					url = 'confirmTakeDelivery'
				}
				//取消订单
				if (name == 'qx') {
					content = '您确定要取消订单'
					url = 'shopcancel'
				}
				//申请退款
				if (name == 'tk') {
					content = '您确定要申请退款'
					url = 'shoprefund'
				}
				uni.showModal({
					title: '操作提示',
					confirmText: '确定',
					confirmColor: '#ff3c37',
					cancelText: '取消',
					content: content,
					success: function(res) {
						if (res.confirm) {
							that.$Http({
								url: url,
								token: true,
								type: 'POST',
								data: {
									order_id: that.dataobj.id
								},
							}).then(res => {
								if (res.code == 1) {
									that.OnDetails()
									that.$forceUpdate();
								} else {
									uni.showToast({
										icon: 'none',
										duration: 2000,
										title: res.msg
									});
								}
							});
						}
					}
				});
			},
			//跳转
			goNext(url) {
				this.$Router.push(url)
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
